﻿var NoComment =
{
	Initialized: false,
	MouseOvered: false,
	Sliding: false,

	BaseURL: "http://www.euronews.net",

	Items: [],
	CurrentItem: 0,

	ReloadTimeout: 1500,
	ReloadTimeoutID: null,

	SwitchTimeout: 15,
	SwitchTimeoutID: null,

	Init: function()
	{
		if (!NoComment.Initialized)
		{
			NoComment.BaseURL = "http://" + GadgetPrefs.getString("BaseURL");

			$("#Container").hover(NoComment.MouseOver, NoComment.MouseOut);
			NoComment.Load();
			NoComment.Initialized = true;

		}
	},

	MouseOver: function()
	{
		NoComment.MouseOvered = true;

		if (!NoComment.Sliding)
		{
			var TextContainer = document.getElementById("TextContainer");

			$(TextContainer).animate(
			{
				"top": "-=" + TextContainer.clientHeight + "px"
			},
			{
				duration: 200
			});
		}
	},

	MouseOut: function()
	{
		var TextContainer = document.getElementById("TextContainer");

		if (!NoComment.Sliding)
		{
			$(TextContainer).animate(
			{
				"top": "+=" + TextContainer.clientHeight + "px"
			},
			{
				duration: 200,
				complete: function() { $("#TextContainer").css("top", "161px"); }
			});
		}
		else
		{
			TextContainer.style.top = "161px";
		}

		NoComment.MouseOvered = false;
	},

	Load: function()
	{
		var params = {};
		params[gadgets.io.RequestParameters.METHOD] = gadgets.io.MethodType.GET;
		params[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.TEXT;
		params[gadgets.io.RequestParameters.REFRESH_INTERVAL] = NoComment.ReloadTimeout;

		gadgets.io.makeRequest(NoComment.BaseURL + "/nocomment/", NoComment.Display, params);
	},

	Display: function(Response)
	{
		if (Response && Response.text)
		{
			var text = Response.text.replace(new RegExp("\n", "g"), "").replace(new RegExp("\r", "g"), "");
			var items = [];

			//last image
			var part = NoComment.GetTextPart(text, new RegExp("<div[^>]+id=\"nocomts\"[^>]*>", "ig"));

			if (part)
			{
				var data = $(part).find("#topStoryImg");
				items.push({
					url: NoComment.BaseURL + data.find("a").attr("href"),
					image: data.find("img").attr("src"),
					title: data.find("a").attr("title")
				});
			}

			
			//prev images
			part = NoComment.GetTextPart(text, new RegExp("<div[^>]+id=\"ilmainNocomments\"[^>]*>", "ig"));

			if (part) {
				var datas = $(part).find("a.topStory");

				for (var i = 0; i < datas.length; i++) {
					data = $(datas[i]);

					items.push({
						url: NoComment.BaseURL + data.attr("href"),
						image: data.find("img").attr("src"),
						title: data.find("img").attr("alt")
					});
				}
			}

			//prev images
			part = NoComment.GetTextPart(text, new RegExp("<div[^>]+id=\"ilScrollContainer\"[^>]*>", "ig"));

			if (part)
			{
				var datas = $(part).find("a.topStory");

				for (var i = 0; i < datas.length; i++)
				{
					data = $(datas[i]);

					items.push({
						url: NoComment.BaseURL + data.attr("href"),
						image: data.find("img").attr("src"),
						title: data.find("img").attr("alt")
					});
				}
			}

			if (items.length > 0)
			{
				window.clearTimeout(NoComment.SwitchTimeoutID);

				NoComment.Items = items.slice(0);
				
				$("#Container").css("visibility", "visible");

				NoComment.CurrentItem = 0;
				NoComment.Image.Display();
				NoComment.SwitchTimeoutID = window.setTimeout(NoComment.Image.Slide, NoComment.SwitchTimeout * 1000);
			}
		}

		NoComment.ReloadTimeoutID = window.setTimeout(NoComment.Load, NoComment.ReloadTimeout * 1000);
		gadgets.window.adjustHeight();
	},

	Image:
	{
		Display: function()
		{
			$("#PhotoImage").attr("src", NoComment.Items[NoComment.CurrentItem].image);
			$("#TextContainer").text(NoComment.Items[NoComment.CurrentItem].title);
			$("#PhotoLink").attr("href", NoComment.Items[NoComment.CurrentItem].url);
			$("#SlideContainer").css("left", $("#SlideContainer").width());

			var NextItem = NoComment.CurrentItem + 1;

			if (NextItem == NoComment.Items.length)
			{
				NoComment.CurrentItem = NextItem = 0;
			}

			$("#SlideImage").attr("src", NoComment.Items[NextItem].image);
		},

		Slide: function()
		{
			if (!NoComment.MouseOvered)
			{
				NoComment.Sliding = true;

				$("#SlideContainer").animate(
				{
					"left": "-=" + $("#SlideContainer").width() + "px"
				},
				{
					duration: 500,
					complete: function()
					{
						NoComment.CurrentItem = NoComment.CurrentItem + 1;
						NoComment.Image.Display();
						NoComment.Sliding = false;
					}
				});
			}

			NoComment.SwitchTimeoutID = window.setTimeout(NoComment.Image.Slide, NoComment.SwitchTimeout * 1000);

		}
	},

	GetTextPart: function(Text, RegEx)
	{
		var result = null;
		var match = Text.match(RegEx);

		if (match)
		{
			try
			{
				var pass = 0;
				var open = Text.indexOf(match[0]), next = open, close;
				var stack = [open];

				while (stack.length > 0 && pass < 1000)
				{
					next = Text.indexOf("div", next + 2);
					var tmp = Text.substr(next - 1, 4);

					if (tmp == "<div")
					{
						stack.push(next);
					}
					else if (tmp == "/div")
					{
						close = next + 3;
						stack.pop();
					}

					pass = pass + 1;
				}

				result = Text.substring(open, close);
			}
			catch (e)
			{
				result = null;
			}
		}

		return (result);

	}

};

var Analytics =
{
	Initialized: false,

	Init: function()
	{
		if (!Analytics.Initialized)
		{
			_gaq = window._gaq || [];
			_gaq.push(["_setAccount", "UA-18423784-3"]);
			_gaq.push(["_trackPageview"]);

			var script = document.createElement("script");
			script.type = "text/javascript";
			script.async = true;
			script.src = (document.location.protocol == "https:" ? "https://ssl" : "http://www") + ".google-analytics.com/ga.js";

			var head = document.getElementsByTagName("head")[0] || document.getElementsByTagName("body")[0];

			if (head)
			{
				head.insertBefore(script, head.firstChild);
			}

			Analytics.Initialized = true;
		}
	}
};